package com.twotiger.and.activity.user;

import java.util.HashMap;

import android.content.Intent;
import android.os.Message;
import android.text.Editable;
import android.text.InputType;
import android.text.Selection;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import app.twotiger.p2p.R;

import com.alibaba.fastjson.JSON;
import com.twotiger.and.Constants;
import com.twotiger.and.activity.base.BaseActivity;
import com.twotiger.and.activity.base.BaseHandler;
import com.twotiger.and.activity.base.SubTitle;
import com.twotiger.and.bean.Basebean;
import com.twotiger.and.bean.ConstantDataUnit;
import com.twotiger.and.util.RegexTool;
import com.twotiger.and.util.StringUtils;
import com.twotiger.and.util.ViewUtils;

/**
 * 
 * @Description: 
 * 重置登录密码 
 * @Author douxy
 * @Date 2015年8月15日 下午3:55:02
 */
public class ModifyLoginPwdPage extends BaseActivity implements OnClickListener {

    private static final int RESETPSWSUCCES = 0;
    private static final int RESETPSWFAIL = 1;

    private SubTitle subTitle;

    private HashMap<String, String> parm;
    private EditText old_pwd;
    private EditText new_pwd;
    private CheckBox new_pwd_plaintext;
    private Button ensure_btn;
    private View forget_pwd;

    @Override
    protected View loadViewLayout(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.modify_login_pwd_layout, null);
        return view;
    }

    @Override
    protected void findViewById(View view) {
        subTitle = new SubTitle(view) {
            @Override
            protected void clickTitle(View v) {
            }

            @Override
            protected void clickRightBtn(View v) {
            }

            @Override
            protected void cickLeftBtn(View v) {
                onBackPress();
            }
        };
        subTitle.setTitle("重置登录密码");

        subTitle.hideBtns();
        subTitle.title_left_bt.setVisibility(View.VISIBLE);

        old_pwd = (EditText) view.findViewById(R.id.old_pwd);
        new_pwd = (EditText) view.findViewById(R.id.new_pwd);
        new_pwd_plaintext = (CheckBox) view.findViewById(R.id.new_pwd_plaintext);
        forget_pwd = view.findViewById(R.id.forget_pwd);
        ensure_btn = (Button) view.findViewById(R.id.ensure_btn);
    }
    
    @Override
    protected void onResume() {
        super.onResume();

        if(!isLogin()){
           finish(); 
        }
    }

    @Override
    public void onClick(View view) {
        String oldP = old_pwd.getText().toString();
        String newP = new_pwd.getText().toString();
        switch (view.getId()) {
        case R.id.ensure_btn:
            if (ViewUtils.isFastDoubleClick(view)) {
                return;
            }
            ConstantDataUnit zz_mm = getConstantDataByKey(Constants.zz_mm);
            if(zz_mm != null){
                if (StringUtils.isEmpty(oldP) || StringUtils.isEmpty(newP) || !RegexTool.match(zz_mm.getVal(), newP)) {
                    showToast("请输入有效密码");
                    return;
                }
            }else {
                if (StringUtils.isEmpty(oldP) || StringUtils.isEmpty(newP) || !RegexTool.match(RegexTool.REG_PASSWORD, newP)) {
                    showToast("请输入有效密码");
                    return;
                }
            }
            if (newP.length() < 8) {
                showToast("请输入正确格式的密码");
                return;
            }
            parm = getParm();
            parm.clear();
            parm.put("oldPwd", oldP);
            parm.put("newPwd", newP);
            parm.put("token", getToken());
            postDate(parm, Constants.RESETPWD, handler, RESETPSWSUCCES, RESETPSWFAIL, true, false,
                false);
            break;
        case R.id.forget_pwd:
            if (ViewUtils.isFastDoubleClick(view)) {
                return;
            }
//            Intent intent = new Intent(this, FindPwdPage.class);
//          startNewActivity(intent, R.anim.push_left_in, R.anim.push_left_out, false);
            Intent intent = new Intent(this, LoginPage.class);
            intent.putExtra("witchLayout", "findpwd");
            startNewActivity(intent, R.anim.push_left_in, R.anim.push_left_out, false);
            break;
        }
    }

    @Override
    protected void setListener() {
        ensure_btn.setOnClickListener(this);
        forget_pwd.setOnClickListener(this);
        new_pwd_plaintext.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    new_pwd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    new_pwd.setInputType(InputType.TYPE_CLASS_TEXT
                                         | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
                Editable etable = new_pwd.getText();
                Selection.setSelection(etable, etable.length());
            }
        });
        handler = new BaseHandler(this) {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                case RESETPSWSUCCES:
                    Basebean result = JSON.parseObject((String) msg.obj, Basebean.class);
                    if (result.isOk()) {
                        finish();
                        showToast("密码修改成功");
                    } else {
                        showToast(result.codeDesc);
                    }
                    old_pwd.setText("");
                    new_pwd.setText("");
                    break;
                case RESETPSWFAIL:
                    showToast("连接错误");
                    break;
                default:
                    break;
                }
                super.handleMessage(msg);
            }
        };
        new_pwd.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s) || s.length() < 8) {
                    ensure_btn.setEnabled(false);
                } else {
                    ensure_btn.setEnabled(true);
                }
            }
        });
    }

    @Override
    protected void processLogic() {

    }

    @Override
    protected void onBackPress() {
        finish();
        overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }

}
